package com.examine.action;

import com.alibaba.fastjson.JSON;
import com.examine.bean.PageResult;
import com.examine.bean.User;
import com.examine.bean.UserReplenish;
import com.examine.serviceImpl.UserReplenishServiceImpl;
import com.examine.serviceImpl.UserServiceImpl;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.examine.utils.UuidUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;

public class UserJsonAction extends ActionSupport {
    private LinkedHashMap mymp;
    private UserServiceImpl userService;
    private UserReplenishServiceImpl userReplenishService;
    private User user;

    public LinkedHashMap getModelMap() {
        return mymp;
    }
    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }

    public String myDefault(){
        mymp = StrResponse.error("访问失败",404);
        return ERROR;
    }

    public String insertUser(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);

        if(StringUtils.isEmpty(user.getUserLoginName())){
            mymp = StrResponse.error("用户名不能为空");
            return ERROR;
        }
        if(StringUtils.isEmpty(user.getUserLoginPwd())){
            mymp = StrResponse.error("密码不能为空");
            return ERROR;
        }



        if(StringUtils.isEmpty(user.getUserLevel())){
            mymp = StrResponse.error("用户类型不能为空");
            return ERROR;
        }

        userService = new UserServiceImpl();

        User user1 = userService.getUserLogin(user);
        if (user1 != null){
            mymp = StrResponse.error("用户已存在");
            return ERROR;
        }

        String id = UuidUtil.getUUID();
        String time = DateUtil.getCurrentTime();
        user.setId(id);
        user.setPlaintPwd(user.getUserLoginPwd());
        user.setUserLevel(user.getUserLevel());
        String password = StrResponse.MD5(user.getUserLoginPwd());
        user.setUserLoginPwd(password);
        user.setCreateTime(time);
        user.setUserLoginName(user.getUserLoginName());
        user.setCreateUserId(UuidUtil.runVerifyCode(10));

        int count = userService.insertSelective(user);

        if (null != user.getUserReplenish()){
            userReplenishService = new UserReplenishServiceImpl();

            user.getUserReplenish().setId(id);
            user.getUserReplenish().setCreateTime(time);
            userReplenishService.insertSelective(user.getUserReplenish());

        }

        if (count == 0){
            mymp = StrResponse.error("用户注册失败");
            return ERROR;
        }

        mymp = StrResponse.success("用户注册成功");
        return SUCCESS;
    }


    public String userLogin(){
        //获取用户前段传入的json数据
        user = JSON.parseObject(StrResponse.getStrResponse().trim().toString(), User.class);
        //判断账号是否为空
        if(StringUtils.isEmpty(user.getUserLoginName())){
            mymp = StrResponse.error("用户名不能为空");
            return ERROR;
        }
        //判断密码是否为空
        if(StringUtils.isEmpty(user.getUserLoginPwd())){
            mymp = StrResponse.error("密码不能为空");
            return ERROR;
        }


        //创建数据库查询实列
        userService = new UserServiceImpl();
        userReplenishService = new UserReplenishServiceImpl();

        //实现用户信息的查询
        User user1 = userService.getUserLogin(user);
        //判断用户的信息是否存在
        if ( null == user1){
            mymp = StrResponse.error("用户名不存在");
            return ERROR;
        }

        //校验密码是否正确
        if (!(user.getUserLoginPwd().equals(user1.getPlaintPwd()))){
            mymp = StrResponse.error("密码错误");
            return ERROR;
        }

        //查询用户的补充信息
        UserReplenish userReplenish = userReplenishService.selectByPrimaryKey(user1.getId());

        user1.setUserReplenish(userReplenish);

        //将用户的信息返回到前段登录业务处理
        mymp = StrResponse.success("登录成功",user1);
        return SUCCESS;
    }

    public String userInfo(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);

        if(StringUtils.isEmpty(user.getId())){
            mymp = StrResponse.error("用户ID不能为空");
            return ERROR;
        }

        userService = new UserServiceImpl();
        userReplenishService = new UserReplenishServiceImpl();
        User user1 = userService.selectByPrimaryKey(user.getId());
        UserReplenish userReplenish = userReplenishService.selectByPrimaryKey(user.getId());

        if (user1 == null && userReplenish == null){
            mymp = StrResponse.error("查询用户信息失败",user1);
            return ERROR;
        }

        user1.setUserReplenish(userReplenish);
        user1.setPlaintPwd(null);
        user1.setUserLoginPwd(null);
        mymp = StrResponse.success("查询用户信息成功", user1);

        return SUCCESS;
    }


    public String allUserList(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);

        userService = new UserServiceImpl();

        List<User> user1 = userService.getAllUserList(user);
        if (user1.size() == 0){
            mymp = StrResponse.error("查询所有用户获取失败");
            return ERROR;
        }

        int countLike = userService.queryCountLike(user);

        PageResult pageResult = new PageResult();

        pageResult.setCurrentPage(user.getPageResult().getCurrentPage());
        pageResult.setPageSize(user.getPageResult().getPageSize());
        pageResult.setTotalSize(countLike);
        pageResult.setItems(user1);

        mymp = StrResponse.success("查询所有用户成功", pageResult);

        return SUCCESS;
    }

    //
    public String deleteUser(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);
        if(StringUtils.isEmpty(user.getId())){
            mymp = StrResponse.error("用户ID不能为空");
            return ERROR;
        }

        userService = new UserServiceImpl();
        int count = userService.deleteByPrimaryKey(user.getId());

        if (null != user.getUserReplenish()){
            userReplenishService = new UserReplenishServiceImpl();

            userReplenishService.deleteByPrimaryKey(user.getUserReplenish().getId());

        }

        if (count == 0){
            mymp = StrResponse.error("删除用户失败");
            return ERROR;
        }
        mymp = StrResponse.success("删除用户成功");
        return SUCCESS;
    }


    public String updateUser(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);
        if(StringUtils.isEmpty(user.getId())){
            mymp = StrResponse.error("用户ID不能为空");
            return ERROR;
        }

        if (user.getUserName() != null){
            user.setUserName(user.getUserName());
        }

        if (user.getUserTel() != null){
            user.setUserTel(user.getUserTel());
        }

        if (user.getUserSex() != null){
            user.setUserSex(user.getUserSex());
        }

        if (user.getUserHeadUrl() != null){
            user.setUserHeadUrl(user.getUserHeadUrl());
        }

        if (user.getUserAddress() != null){
            user.setUserAddress(user.getUserAddress());
        }

        if (user.getUserBrithday() != null){
            user.setUserBrithday(user.getUserBrithday());
        }

        if (user.getUserEmail() != null){
            user.setUserEmail(user.getUserEmail());
        }

        //当传入的密码不为空时，进行的是修改密码操作
        if (!StringUtils.isEmpty(user.getUserLoginPwd())){

            user.setPlaintPwd(user.getUserLoginPwd());
            //将密码进行md5加密
            String password = StrResponse.MD5(user.getUserLoginPwd());
            user.setUserLoginPwd(password);
        }


        String time = DateUtil.getCurrentTime();

        //更新修改时间
        user.setUpdateTime(time);

        userService = new UserServiceImpl();
        int count = userService.updateByPrimaryKeySelective(user);

        if (null != user.getUserReplenish()){
            userReplenishService = new UserReplenishServiceImpl();

            user.getUserReplenish().setCreateTime(time);
            userReplenishService.updateByPrimaryKeySelective(user.getUserReplenish());

        }

        if (count == 0){
            mymp = StrResponse.error("修改用户失败");
            return ERROR;
        }

        mymp = StrResponse.success("修改用户成功");
        return SUCCESS;
    }


    public String updateUserPwd(){
        user = JSON.parseObject(StrResponse.getStrResponse(), User.class);
        if(StringUtils.isEmpty(user.getUserLoginName())){
            mymp = StrResponse.error("用户名不能为空");
            return ERROR;
        }

        userService = new UserServiceImpl();

        //验证用户是否存在，以便修改密码
        User user1 = userService.getUserLogin(user);
        if ( null == user1){
            mymp = StrResponse.error("用户名不存在");
            return ERROR;
        }


        if (!(user.getUserEmail().equals(user1.getUserEmail()))){
            mymp = StrResponse.error("您的邮箱不正确");
            return ERROR;
        }

        user.setId(user1.getId());
        user.setPlaintPwd(user.getUserLoginPwd());
        String password = StrResponse.MD5(user.getUserLoginPwd());
        user.setUserLoginPwd(password);
        user.setUpdateTime(DateUtil.getCurrentTime());

        userService = new UserServiceImpl();
        int count = userService.updateByPrimaryKeySelective(user);

        if (count == 0){
            mymp = StrResponse.error("修改用户密码失败");
            return ERROR;
        }
        mymp = StrResponse.success("修改用户密码成功");
        return SUCCESS;
    }


}
